<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category  Evebit
 * @package   App_Model
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: Abstract.php 130 2011-03-18 03:10:02Z cheng.wei $$
 * @copyright Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Abstract resource model
 * 
 * @package App_Model
 * @author  Cheng Wei <berlios.o@gmail.com>
 * @version $$Id: Abstract.php 130 2011-03-18 03:10:02Z cheng.wei $$
 */
abstract class Evebit_Model_Resource_Abstract
{
    public function __construct()
    {
        $this->_construct();
    }

    /**
     * Resource initialization
     */
    abstract protected function _construct();

    /**
     * Retrieve connection for read data
     */
    abstract protected function _getReadAdapter();

    /**
     * Retrieve connection for write data
     */
    abstract protected function _getWriteAdapter();

    /**
     * Start resource transaction
     *
     * @return Mage_Core_Model_Resource_Abstract
     */
    public function beginTransaction()
    {
        $this->_getWriteAdapter()->beginTransaction();
        return $this;
    }

    /**
     * Commit resource transaction
     *
     * @return Mage_Core_Model_Resource_Abstract
     */
    public function commit()
    {
        $this->_getWriteAdapter()->commit();
        return $this;
    }

    /**
     * Roll back resource transaction
     *
     * @return Mage_Core_Model_Resource_Abstract
     */
    public function rollBack()
    {
        $this->_getWriteAdapter()->rollBack();
        return $this;
    }

    /**
     * Format date to internal format
     *
     * @param   string | Zend_Date $date
     * @param   bool $includeTime
     * @return  string
     */
    public function formatDate($date, $includeTime=true)
    {
        if ($date instanceof Zend_Date) {
            if ($includeTime) {
                return $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
            }
            else {
                return $date->toString(Varien_Date::DATE_INTERNAL_FORMAT);
            }
        }

    	if (empty($date)) {
    		return new Zend_Db_Expr('NULL');
    	}

        if (!is_numeric($date)) {
            $date = strtotime($date);
        }
        if ($includeTime) {
            return date('Y-m-d H:i:s', $date);
        }
        else {
            return date('Y-m-d', $date);
        }
    }

    public function mktime($str)
    {
        return  strtotime($str);
    }
}
